package com.mrd.gtimp.query.action;

import com.mrd.constant.ItfConstants;
import com.mrd.framework.ui.filter.Filter;
import com.mrd.framework.ui.filter.FilterGroupOption;
import com.mrd.framework.ui.filter.SearchRule;
import com.mrd.framework.ui.filter.SearchRuleOption;
import com.mrd.gtimp.base.entity.Company;
import com.mrd.gtimp.base.entity.SystemCopInterface;
import com.mrd.gtimp.base.service.CompanyBiz;
import com.mrd.gtimp.base.service.SystemCopInterfaceBiz;
import com.mrd.gtimp.query.dto.CtQueryInfo;
import com.mrd.gtimp.query.dto.VehicleQueryInfo;
import com.mrd.gtimp.sup.service.SupComConfBiz;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.ArrayList;
import java.util.List;

import javax.annotation.Resource;

/**
 * <p> application name: gtimp <p> copyright: Copyright@2012 明睿达科技有限公司
 */

/**
 * @author luob
 */

@Controller
@RequestMapping("/query")
public class BizInfoQueryAction {
    private static final long serialVersionUID = 1L;
    @Resource
    private SystemCopInterfaceBiz systemCopInterfaceBiz;

    @Resource
    private CompanyBiz companyBiz;

    @Resource
    private SupComConfBiz supComConfBiz;

    private List<CtQueryInfo> ctQueryInfos;

    private List<VehicleQueryInfo> vehicleQueryInfos;

    private String jsonString;

    /**
     * 货柜堆存量查询入口
     */
    @RequestMapping("/bizInfoQuery-ctMain")
    public String ctMain() {
        return "/gtimp/query/ctInfoQueryList";
    }

    /**
     * 运力查询入口
     */
    @RequestMapping("/bizInfoQuery-vehicleMain")
    public String vehicleMain() {
        return "/gtimp/query/veInfoQueryList";
    }

    /**
     * 货柜堆存量数据查询方法
     */
    @RequestMapping("/bizInfoQuery-findCtInfo")
    @ResponseBody
    public String findCtInfo(Filter filters) {
        String msg = "";
        // check filter
        if (null != filters && filters.getRules() != null) {
            String comId = "";
            String shipComId = "";
            boolean hasShipCom = false;
            boolean hasOwner = false;
            for (SearchRule rule : filters.getRules()) {
                if ("shipCompany.id".equals(rule.getField())) {
                    hasShipCom = true;
                    shipComId = rule.getData().toString();
                } else if ("owner.id".equals(rule.getField())) {
                    hasOwner = true;
                    comId = rule.getData().toString();
                }
            }
            if (hasOwner) {
                // check itf
                Filter itfFilter = new Filter();
                itfFilter.setGroupOp(FilterGroupOption.AND);
                SearchRule rule = new SearchRule("company.id", SearchRuleOption.eq, comId);
                itfFilter.addRule(rule);
                rule = new SearchRule("systemInterface.id", SearchRuleOption.eq, ItfConstants.CT_QUERY_ITF_ID);
                itfFilter.addRule(rule);
                rule = new SearchRule("use_state", SearchRuleOption.eq, ItfConstants.USE_STATE);
                itfFilter.addRule(rule);
                try {
                    List<SystemCopInterface> itfs = systemCopInterfaceBiz
                            .findEntityByFilter(0, 1, itfFilter, null);
                    ctQueryInfos = new ArrayList<CtQueryInfo>();
                    Company owner = companyBiz.findEntityById(comId);

                    Company shipCompany = null;
                    if (hasShipCom) {
                        shipCompany = companyBiz.findEntityById(shipComId);
                    }

                    if (null != itfs && itfs.size() == 1) {
                        // get result
                        String url = itfs.get(0).getURL();
                        //// TODO: 2016/8/15  page service
//                        BizInfoQueryDTO dto = new BizInfoQueryDTO();
//                        dto.setUrl(url);
//                        page = initPage(0);
//                        dto.setCurrentPage(page == null ? 1 : page.getCurrentPage());
//                        dto.setTotle(page == null ? 0 : page.getTotle());
//                        dto.setPageSize(page == null ? 15 : page.getPageSize());
//                        if (hasShipCom) {
//                            dto.setShipCompanyKey(shipCompany.getId());
//                        }
//                        BizInfoResultDTO result = new BizInfoQueryClient().findCtInfo(dto);
//                        page = initPage(result.getTotle());
//                        List<CtResult> rlist = result.getCtResultList();
//                        String ctLevel = supComConfBiz.findCtLevel(comId);
//                        for (int i = 0; i < rlist.size(); i++) {
//                            CtResult obj = rlist.get(i);
//                            CtQueryInfo info = new CtQueryInfo();
//                            info.setqTime(new Date());
//                            info.setOwner(owner);
//                            info.setShipCompany(companyBiz.findEntityById(obj.getShipCompanyKey()));
//                            info.setResult(supComConfBiz.ctCountsToStringByCtLevel(obj.getCtCount(), ctLevel));
//                            ctQueryInfos.add(info);
//                        }
                    } else {
                        msg = "该供应商未提供该查询功能";
                        return "msg";
                    }
                } catch (Exception e) {
                    e.printStackTrace();
                }

            } else {
                msg = "请选择供应商进行查询";
                return "msg";
            }
        }
        //// TODO: 2016/8/15  json
//        return "ctGridData";
        return null;
    }

    /**
     * 运力数据查询方法
     */
    @RequestMapping("/bizInfoQuery-findVehicleInfo")
    @ResponseBody
    public String findVehicleInfo(Filter filters) {
        String msg = "";
        // check filter
        if (null != filters && filters.getRules() != null) {
            String comId = "";
            boolean hasOwner = false;
            for (SearchRule rule : filters.getRules()) {
                if ("owner.id".equals(rule.getField())) {
                    hasOwner = true;
                    comId = rule.getData().toString();
                    break;
                }
            }
            if (hasOwner) {
                // check itf
                Filter itfFilter = new Filter();
                itfFilter.setGroupOp(FilterGroupOption.AND);
                SearchRule rule = new SearchRule("company.id", SearchRuleOption.eq, comId);
                itfFilter.addRule(rule);
                rule = new SearchRule("systemInterface.id", SearchRuleOption.eq, ItfConstants.VEHICLE_QUERY_ITF_ID);
                itfFilter.addRule(rule);
                rule = new SearchRule("use_state", SearchRuleOption.eq, ItfConstants.USE_STATE);
                itfFilter.addRule(rule);
                try {
                    List<SystemCopInterface> itfs = systemCopInterfaceBiz
                            .findEntityByFilter(0, 1, itfFilter, null);
                    vehicleQueryInfos = new ArrayList<VehicleQueryInfo>();
                    Company owner = companyBiz.findEntityById(comId);

                    if (null != itfs && itfs.size() == 1) {
                        // get result
                        String url = itfs.get(0).getURL();
                        //// TODO: 2016/8/15  service
//                        BizInfoQueryDTO dto = new BizInfoQueryDTO();
//                        dto.setUrl(url);
//                        /*
//                         * page = initPage(0);
//						 * dto.setCurrentPage(page == null ? 1 : page.getCurrentPage());
//						 * dto.setTotle(page == null ? 0 : page.getTotle());
//						 * dto.setPageSize(page == null ? 15 : page.getPageSize());
//						 */
//                        BizInfoResultDTO result = new BizInfoQueryClient().findVehicleInfo(dto);
//                        int vehicleCount = result.getVehicleCount();
//                        String veLevel = supComConfBiz.findVehicleLevel(comId);
//                        VehicleQueryInfo info = new VehicleQueryInfo();
//                        info.setResult(supComConfBiz
//                                .ctCountsToStringByCtLevel(vehicleCount, veLevel));
//                        info.setOwner(owner);
//                        info.setqTime(new Date());
//                        // page = initPage(result.getTotle());
//                        vehicleQueryInfos.add(info);
                    } else {
                        msg = "该供应商未提供该查询功能";
                        return "msg";
                    }
                } catch (Exception e) {
                    e.printStackTrace();
                }

            } else {
                msg = "请选择供应商进行查询";
                return "msg";
            }
        }
        //// TODO: 2016/8/15  json
//        return "veGridData";
        return null;
    }

    public String getJsonString() {
        return jsonString;
    }

    public void setJsonString(String jsonString) {
        this.jsonString = jsonString;
    }

    public void setSystemCopInterfaceBiz(SystemCopInterfaceBiz systemCopInterfaceBiz) {
        this.systemCopInterfaceBiz = systemCopInterfaceBiz;
    }

    public void setCompanyBiz(CompanyBiz companyBiz) {
        this.companyBiz = companyBiz;
    }

    public void setSupComConfBiz(SupComConfBiz supComConfBiz) {
        this.supComConfBiz = supComConfBiz;
    }

    public List<CtQueryInfo> getCtQueryInfos() {
        return ctQueryInfos;
    }

    public void setCtQueryInfos(List<CtQueryInfo> ctQueryInfos) {
        this.ctQueryInfos = ctQueryInfos;
    }

    public List<VehicleQueryInfo> getVehicleQueryInfos() {
        return vehicleQueryInfos;
    }

    public void setVehicleQueryInfos(List<VehicleQueryInfo> vehicleQueryInfos) {
        this.vehicleQueryInfos = vehicleQueryInfos;
    }
}
